package com.kfm.web;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.kfm.control.JdbcSelect;
import com.kfm.map.ServerTA;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/server")
public class ServerWeb extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String sql = "select * from serverta";
        List<ServerTA> selectinfo = JdbcSelect.selectinfo(new ServerTA(), sql);
        JSONArray array = JSONUtil.createArray();
        for (ServerTA serverTA : selectinfo) {
            String jsonStr = JSONUtil.toJsonStr(serverTA);
            array.add(jsonStr);
        }
        String jsonStr = JSONUtil.toJsonStr(array);
        writer.write(jsonStr);
    }
}
